package com.sgb.kjwl.view.ui.activity.home;

import androidx.databinding.DataBindingUtil;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentSearchBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.model.entity.StoreEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteInfoActivity;
import com.sgb.kjwl.view.ui.adapter.home.SearchCompanyAdapter;
import com.sgb.kjwl.viewmodel.home.SearchCompanyFragmentModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

/**
 * 搜索企业
 */
public class SearchCompanyFragment extends AppFragment {
    public static final String TAG = "SearchCompanyFragment";
    public static final String KEY_DATATYPE = "data_type";
    private FragmentSearchBinding binding;
    @Inject
    SearchCompanyFragmentModel viewModel;

    private String searchText = "";

    private LinearLayoutManager linearLayoutManager;
    private SearchCompanyAdapter searchCompanyAdapter;

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    public static SearchCompanyFragment newInstance() {
        SearchCompanyFragment fragment = new SearchCompanyFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_search, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initData();
    }

    private void initData() {
        binding.emptyView.blankView.setVisibility(View.VISIBLE);
        binding.recyclerView.setVisibility(View.GONE);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(linearLayoutManager);


        //发现供应商
        if (searchCompanyAdapter == null) {
            searchCompanyAdapter = new SearchCompanyAdapter(getActivity(), null);
        }
        binding.recyclerView.setAdapter(searchCompanyAdapter);
        searchCompanyAdapter.setOnItemClick(new AdapterOnItemClick<StoreEntity>() {
            @Override
            public void onItemClick(StoreEntity storeEntity, int position) {
                if (TextUtils.isEmpty(storeEntity.getNo())) {
                    ToastUtil.show(getActivity(), "企业店铺No不能为空");
                    return;
                }
                WebsiteInfoActivity.start(getActivity(), storeEntity.getNo(), WebsiteInfoActivity.WEBSITE_FOR_COMPANY, 0, 0);
            }
        });
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.searchStoreCatogories(SearchActivity4.SEARCHTYPE_COMPANY, searchText, false);
            }

            @Override
            public void onLoadMore() {
                viewModel.searchStoreCatogories(SearchActivity4.SEARCHTYPE_COMPANY, searchText, true);
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LeaseEvent event) {
        if (event.getMessageTag() == EventConstant.SEARCH_ENTERPRISE) {
            searchText = (String) event.getMessageData();
            if (this.getActivity() instanceof SearchActivity4) {
                viewModel.searchStoreCatogories(SearchActivity4.SEARCHTYPE_COMPANY, searchText, false);
            }
            EventBus.getDefault().removeStickyEvent(event);
        }
    }

    public void refresh(List<StoreEntity> storeEntityList, boolean isLast) {
        binding.emptyView.blankView.setVisibility(View.GONE);
        binding.recyclerView.setVisibility(View.VISIBLE);
        binding.recyclerView.setLoadingMoreEnabled(true);
        searchCompanyAdapter.setDatas(storeEntityList);
        binding.recyclerView.refreshComplete();
        if (isLast) {
            binding.recyclerView.setLoadingMoreEnabled(false);
        }
    }

    public void empty() {
        binding.recyclerView.refreshComplete();
        binding.recyclerView.setVisibility(View.GONE);
        binding.emptyView.blankView.setVisibility(View.VISIBLE);
        searchCompanyAdapter.setDatas(null);
    }

    public void loadMore(List<StoreEntity> storeEntityList, boolean isLast) {
        searchCompanyAdapter.addDatas(storeEntityList);
        binding.recyclerView.loadMoreComplete();
        if (isLast) {
            binding.recyclerView.setLoadingMoreEnabled(false);
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
